/**
 * Created by python on 19-8-29.
 */

var app = new Vue({
    el:"#personcount",
    data:{
        // 页面中需要使用到的数据，键值对
        HOST,
        user_id: sessionStorage.user_id || localStorage.user_id,
        token: sessionStorage.token || localStorage.token,
        password:'',
        nick_name:'',
        address :'',
        company :'',
        persintro:'',
        email:"",
        mobile :'',
        persurl :true,
        name:true,
        persurlname:true,
        mobilename:true,
        emailname:true,
        passwordname:true,
        changeemail:false,
        changemobile:false,
        changepersurl:false,
        changepassword:false,
        changename:false,

    },
    mounted:function () {
        // 一加载就需要做的，直接是代码
        if (this.user_id && this.token) {
            axios.get(this.HOST + 'personalcount/',
                {

                    headers: {
                        'Authorization': 'JWT ' + this.token
                    },

                    responseType: 'json',

                })
                .then(response => {
                    console.log(response);
                    this.nick_name = response.data.nick_name;
                    this.persurl = response.data.persurl;
                    this.mobile = response.data.mobile;
                    this.email=response.data.user_id.email;
                })
        }
    },
    methods:{
        // 需要用到的函数，键值对 ，键是名称，值是匿名函数
        show_name: function () {
            this.name = true;
            this.changename = false
            return false

        },
        name_set: function () {
            this.name = false;
            this.changename = true;
            this.$nextTick(() => {
                this.$refs.gain1.focus()
            })
        },
        persurl_set: function () {
            this.persurlname = false;
            this.changepersurl = true;
            this.$nextTick(() => {
                this.$refs.gain2.focus()
            })
        },
        show_persurl: function () {
            this.persurlname = true;
            this.changepersurl = false

        },
          show_mobile: function () {
            this.mobilename = true;
            this.changemobile = false

        },
        mobile_set: function () {
            this.mobilename = false;
            this.changemobile = true;
            this.$nextTick(() => {
                this.$refs.gain3.focus()
            })
        },


        show_email: function () {
            this.emailname = true;
            this.changeemail = false

        },
        email_set: function () {
            this.emailname = false;
            this.changeemail = true;
            this.$nextTick(() => {
                this.$refs.gain4.focus()
            })
        },

        show_password: function () {
            this.passwordname = true;
            this.changepassword = false

        },

        password_set: function () {
            this.passwordname = false;
            this.changepassword = true;
            this.$nextTick(() => {
                this.$refs.gain5.focus()
            })

        },
           // 修改邮箱
        on_submit_email: function(){
            alert('heh')


            if (this.email) {
                axios.patch(this.HOST + 'email/', {
                        email: this.email
                    },
                    {

                        headers: {
                            'Authorization': 'JWT ' + this.token
                        },

                        responseType: 'json',

                    })
                    .then(response => {
                        console.log(response.data)

                        location.href = '/person-account.html';
                    })
                    .catch(error => {
                        if (error.response.status == 400) {
                            this.error_pwd_message = '数据错误';
                        } else {
                            this.error_pwd_message = '服务器错误';
                        }
                        this.error_pwd = true;
                    })
            }

            return false
        },
                // 修改个人网址
        on_submit_persurl: function() {
            alert('heh')


            if (this.persurl) {
                axios.patch(this.HOST + 'persurl/', {
                        persurl: this.persurl
                    },
                    {

                        headers: {
                            'Authorization': 'JWT ' + this.token
                        },

                        responseType: 'json',

                    })
                    .then(response => {
                        location.href = '/person-account.html';
                    })
                    .catch(error => {
                        if (error.response.status == 400) {
                            this.error_pwd_message = '数据错误';
                        } else {
                            this.error_pwd_message = '服务器错误';
                        }
                        this.error_pwd = true;
                    })
            }
        },
                      // 修改昵称
        on_submit_name: function() {
            alert('heh')


            if (this.nick_name) {
                axios.patch(this.HOST + 'nickname/', {
                        nick_name: this.nick_name
                    },
                    {

                        headers: {
                            'Authorization': 'JWT ' + this.token
                        },

                        responseType: 'json',

                    })
                    .then(response => {
                        location.href = '/person-account.html';
                    })
                    .catch(error => {
                        if (error.response.status == 400) {
                            this.error_pwd_message = '数据错误';
                        } else {
                            this.error_pwd_message = '服务器错误';
                        }
                        this.error_pwd = true;
                    })
            }
        },
        // 修改电话
        on_submit_mobile: function() {
            alert('heh')


            if (this.mobile) {
                axios.patch(this.HOST + 'mobile/', {
                        moile: this.mobile
                    },
                    {

                        headers: {
                            'Authorization': 'JWT ' + this.token
                        },

                        responseType: 'json',

                    })
                    .then(response => {
                        location.href = '/person-account.html';
                    })
                    .catch(error => {
                        if (error.response.status == 400) {
                            this.error_pwd_message = '数据错误';
                        } else {
                            this.error_pwd_message = '服务器错误';
                        }
                        this.error_pwd = true;
                    })
            }
        },
                // 修改密码
        on_submit_password: function() {
            alert('heh')


            if (this.password) {
                axios.patch(this.HOST + 'password/', {
                        password: this.password
                    },
                    {

                        headers: {
                            'Authorization': 'JWT ' + this.token
                        },

                        responseType: 'json',

                    })
                    .then(response => {
                        location.href = '/person-account.html';
                    })
                    .catch(error => {
                        if (error.response.status == 400) {
                            this.error_pwd_message = '数据错误';
                        } else {
                            this.error_pwd_message = '服务器错误';
                        }
                        this.error_pwd = true;
                    })
            }
        },
    },
})